package bg.code.commons.plugin.ui.table;

import bg.code.commons.builder.dto.ResultDTO;
import bg.code.commons.builder.utils.Strings;
import com.intellij.ui.components.JBPanel;

import javax.swing.*;
import java.awt.*;

import static bg.code.commons.plugin.Cons.*;

/**
 * @author 郑兴泉 956607644@qq.com
 * @data 2020/8/11
 * 描述：
 */
public final class ClassJBPanel implements ICustomJBPanel<JTextField> {

    private final JLabel label;
    private final JTextField classField;

    public ClassJBPanel() {
        this.label = new JLabel(CLASS_REQUEST_PARAM_PATH);
        this.classField = new JTextField();
        this.classField.setToolTipText(CLASS_REQUEST_PARAM_PATH);
    }

    @Override
    public JComponent getComponent() {
        JBPanel panel = new JBPanel();
        panel.setLayout(new BorderLayout());
        panel.setBorder(BorderFactory.createTitledBorder(CLASS_REQUEST_PARAM_NAME));
        panel.add(this.label, BorderLayout.WEST);
        panel.add(this.classField, BorderLayout.CENTER);
        return panel;
    }

    @Override
    public JTextField getObj() {
        return this.classField;
    }

    @Override
    public String getData() {
        return classField.getText();
    }

    public void setClassFieldText(String text) {
        this.classField.setText(text);
    }

    @Override
    public ResultDTO errorMsg() {
        if (Strings.isBlank(classField.getText())) {
            return new ResultDTO(false, CLASS_REQUEST_PARAM_PATH_ERROR_MSG);
        }
        return new ResultDTO(true, "");
    }
}
